home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / gl_start.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-16  |  3.6 KB  |  127 lines

  1. //
  2. // "$Id: gl_start.cxx,v 1.6.2.1 1999/09/16 05:34:27 bill Exp $"
  3. //
  4. // OpenGL context routines for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // You MUST use gl_visual() to select the default visual before doing
  27. // show() of any windows.  Mesa will crash if you try to use a visual
  28. // not returned by glxChooseVisual.
  29.  
  30. // This does not work with Fl_Double_Window's!  It will try to draw
  31. // into the front buffer.  Depending on the system this will either
  32. // crash or do nothing (when pixmaps are being used as back buffer
  33. // and GL is being done by hardware), work correctly (when GL is done
  34. // with software, such as Mesa), or draw into the front buffer and
  35. // be erased when the buffers are swapped (when double buffer hardware
  36. // is being used)
  37.  
  38. #include <config.h>
  39. #if HAVE_GL
  40.  
  41. #include <FL/Fl.H>
  42. #include <FL/Fl_Window.H>
  43. #include <FL/x.H>
  44. #include <FL/fl_draw.H>
  45.  
  46. #include "Fl_Gl_Choice.H"
  47.  
  48. extern GLXContext fl_first_context; // in Fl_Gl_Choice.C
  49. extern int fl_clip_state_number; // in fl_rect.C
  50.  
  51. static GLXContext context;
  52. static int clip_state_number=-1;
  53. static int pw, ph;
  54.  
  55. #ifdef WIN32
  56. static int default_mode;
  57. #endif
  58.  
  59. Region XRectangleRegion(int x, int y, int w, int h); // in fl_rect.C
  60.  
  61. void gl_start() {
  62. #ifdef WIN32
  63.   HDC hdc = fl_private_dc(Fl_Window::current(), default_mode,0);
  64. #endif
  65.   if (!context) {
  66. #ifdef WIN32
  67.     context = wglCreateContext(hdc);
  68.     if (!fl_first_context) fl_first_context = context;
  69.     else wglShareLists(fl_first_context, context);
  70. #else
  71.     context = glXCreateContext(fl_display, fl_visual, fl_first_context, 1);
  72.     if (!context) Fl::fatal("OpenGL does not support this visual");
  73.     if (!fl_first_context) fl_first_context = context;
  74. #endif
  75.   }
  76.   fl_set_gl_context(Fl_Window::current(), context);
  77. #ifndef WIN32
  78.   glXWaitX();
  79. #endif
  80.   if (pw != Fl_Window::current()->w() || ph != Fl_Window::current()->h()) {
  81.     pw = Fl_Window::current()->w();
  82.     ph = Fl_Window::current()->h();
  83.     glLoadIdentity();
  84.     glViewport(0, 0, pw, ph);
  85.     glOrtho(0, pw, 0, ph, -1, 1);
  86.     glDrawBuffer(GL_FRONT);
  87.   }
  88.   if (clip_state_number != fl_clip_state_number) {
  89.     clip_state_number = fl_clip_state_number;
  90.     int x, y, w, h;
  91.     if (fl_clip_box(0, 0, Fl_Window::current()->w(), Fl_Window::current()->h(),
  92.             x, y, w, h)) {
  93.       fl_clip_region(XRectangleRegion(x,y,w,h));
  94.       glScissor(x, Fl_Window::current()->h()-(y+h), w, h);
  95.       glEnable(GL_SCISSOR_TEST);
  96.     } else {
  97.       glDisable(GL_SCISSOR_TEST);
  98.     }
  99.   }
  100. }
  101.  
  102. void gl_finish() {
  103. #ifdef WIN32
  104.   glFlush();
  105. #else
  106.   glXWaitGL();
  107. #endif
  108. }
  109.  
  110. int Fl::gl_visual(int mode, int *alist) {
  111. #ifdef WIN32
  112.   default_mode = mode;
  113. #else
  114.   Fl_Gl_Choice *c = Fl_Gl_Choice::find(mode,alist);
  115.   if (!c) return 0;
  116.   fl_visual = c->vis;
  117.   fl_colormap = c->colormap;
  118. #endif
  119.   return 1;
  120. }
  121.  
  122. #endif
  123.  
  124. //
  125. // End of "$Id: gl_start.cxx,v 1.6.2.1 1999/09/16 05:34:27 bill Exp $".
  126. //
  127.